﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.Composition;
using Core.DataAccess.IRepositories;

namespace Core.DataAccess.Repositories
{
    [Export(typeof(ILevelRepository))]
    public class LevelRepository : ILevelRepository
    {
        private Connection conn;

        public LevelRepository()
        {
            conn = new Connection();
        }

        public string GetLevelPathByLevelID(int levelID)
        {
            string levelName = "";
            using (MXHEntities context = conn.GetContext())
            {
                var item = from le in context.Levels
                           where le.LevelID == levelID
                           select le;
                if (item.Count() > 0)
                {
                    Level level = item.FirstOrDefault();
                    levelName = level.LevelPath;
                }
            }
            return levelName;
        }

        public int GetNextScoreUpByLevelID(int levelID)
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from le in context.Levels
                           where le.LevelID == levelID
                           select le;
                return item.Count() > 0 ? Convert.ToInt32(item.FirstOrDefault().ScoreUp) : -1;
            }
        }

        public int NumberLevel()
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from le in context.Levels                           
                           select le;
                return item.Count();
            }
        }

        public List<Level> GetAllLevels()
        {
            using (MXHEntities context = conn.GetContext())
            {
                var item = from lv in context.Levels
                           select lv;
                return item.Count() > 0 ? item.ToList() : null;
            }
        }

        public string GetLevelNameByLevelID(int levelID)
        {            
            using (MXHEntities context = conn.GetContext())
            {
                var item = from lv in context.Levels
                           where lv.LevelID == levelID
                           select lv;
                return item.Count() > 0 ? item.FirstOrDefault().LevelName : "";
            }
        }        
    }
}
